<template>
  <el-form
    ref="loginForm"
    :model="loginUser"
    :rules="rules"
    label-width="100px"
    class="loginForm sign-in-form"
  >
    <el-form-item label="邮箱" prop="email">
      <el-input
        v-model="loginUser.email"
        placeholder="Enter Email..."
      ></el-input>
    </el-form-item>
    <el-form-item label="密码" prop="password">
      <el-input
        v-model="loginUser.password"
        type="password"
        placeholder="Enter Password..."
      ></el-input>
    </el-form-item>

    <el-form-item>
      <el-button
        @click="handleLogin('loginForm')"
        type="primary"
        class="submit-btn"
        >提交</el-button
      >
    </el-form-item>

    <!-- 找回密码 prevent拦截默认事件 -->
    <div class="tiparea">
      <p>忘记密码？ <a @click.prevent="handleForgot()">立即找回</a></p>
    </div>
  </el-form>
</template>

<script lang="ts">
import { getCurrentInstance } from "vue";
import { useRouter } from "vue-router";
import axios from 'axios';
export default {
  props: {
    loginUser: { //双向绑定数据
      type: Object,
      required: true,
    },
    rules: { // 校验数据
      type: Object,
      required: true,
    },
  },
  setup(props:any) {
    console.log(getCurrentInstance());
    
    // @ts-ignore
    const { ctx } = getCurrentInstance();
    const  router  =useRouter() 
    // 触发登录方法
    const handleLogin = (formName: string) => {
      ctx.$refs[formName].validate((valid: boolean) => {
          // //路由跳转
          // router.push('/')
        if (valid) {
          axios('api/v1/auth/login',props.loginUser).then((res:any)=>{
           //登录成功，存储token
          //  const {token} = res.data 相当于const token = res.data.token
          //解构
           const {token} = res.data
           //存储
           localStorage.setItem('msToken',token)
           //页面跳转
           router.push('/')
          })
        } else {
          console.log("error submit!!");
          return false;
        }
      })
    };
     //立即找回
      const handleForgot = () =>{
        router.push('/forgotpassword')
      }
    return { handleLogin,handleForgot};
  },
};
</script>
<style scoped>
/* form */
.loginForm {
  margin-top: 20px;
  background-color: #fff;
  padding: 20px 40px 20px 20px;
  border-radius: 5px;
  box-shadow: 0px 5px 10px #cccc;
}

.submit-btn {
  width: 100%;
}
.tiparea {
  text-align: right;
  font-size: 12px;
  color: #333;
}
.tiparea p a {
  color: #409eff;
}
</style>